package com.umfwechat.wx.service.serviceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.umfwechat.common.constant.CacheConsts;
import com.umfwechat.util.PropertyUtil;
import com.umfwechat.util.RedisUtil;
import com.umfwechat.util.StringUtil;
import com.umfwechat.util.wx.WeixinUtil;
import com.umfwechat.util.wx.weChatGroupUtil;
import com.umfwechat.wx.pojo.AccessTokenPojo;
import com.umfwechat.wx.pojo.TicketPojo;
import com.umfwechat.wx.pojo.Button;
import com.umfwechat.wx.pojo.ComplexButton;
import com.umfwechat.wx.pojo.CustomMatchrule;
import com.umfwechat.wx.pojo.CustomMenu;
import com.umfwechat.wx.pojo.Menu;
import com.umfwechat.wx.pojo.TemplateDate;
import com.umfwechat.wx.pojo.ViewButton;
import com.umfwechat.wx.pojo.WxTemplate;
import com.umfwechat.wx.service.WeixinService;

/**
 * @desc 微信Service接口
 * @author Wangzhilong
 * @date 2017-5-13 下午5:59:29
 */
@Service
public class WeixinServiceImpl implements WeixinService {

	private static Logger logger = LoggerFactory.getLogger(WeixinServiceImpl.class);
	// 域名
	private static String DOMAIN = PropertyUtil.getProperties("umfWeChatPlat_appConf.properties").getProperty("saas.wx.domain");
	// 微信appid
	private static String APPID2 = PropertyUtil.getProperties("umfWeChatPlat_appConf.properties").getProperty("saas.wx.appid");
	// 微信公众号appsecret
	private static String APPSECRET2 = PropertyUtil.getProperties("umfWeChatPlat_appConf.properties").getProperty("saas.wx.appsecret");

	private static String URL1 = "https://" + DOMAIN + "/umfWeChatPlat";

	private static String URL="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+APPID2+"&redirect_uri=https%3a%2f%2f"+DOMAIN+"%2fumfWeChatPlat";
	
	private static String URL_SIGN="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+APPID2+"&redirect_uri=https%3a%2f%2f"+DOMAIN+"%2fumfWeChatPlat%2fh5%2foAuchController%2fsettlementQueryCard&response_type=code&scope=snsapi_base&state=3&connect_redirect=1";
	/**					
	 * 获取access_token
	 * @return
	 */
	@Override
	public AccessTokenPojo getAccessToken() {
		AccessTokenPojo tokenPojo = new AccessTokenPojo();
		try {
			String string = RedisUtil.get(CacheConsts.WX + "access");
			logger.info("从redis中获取微信accessToken!!!!" + string);
			boolean expires = this.isExpires();
			logger.info("生成微信accessToken的时间与实时时间差>微信accessToken有效期!!!" + expires);
			if (StringUtil.isEmpty(string)||expires) {
				logger.info("重新调用微信获取accessToken!!!!");
				tokenPojo = WeixinUtil.getAccessToken(APPID2, APPSECRET2);
				logger.info("accessToken!!!！！！！！！！！！！！！" + tokenPojo);
				String token = tokenPojo.getAccessToken();
				RedisUtil.set(CacheConsts.WX + "access", 1800, token);
				logger.info("把accessToken存入redis中!!!!" + token);
				long time = new Date().getTime();
				String of = String.valueOf(time);
				RedisUtil.set(CacheConsts.WX + "accesstime",2000,of);
				logger.info("把重新生成accessToken时的时间存入redis中!!!!" + of);
				return tokenPojo;
			} else {
				tokenPojo.setAccessToken(string);
			}
			return tokenPojo;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(String.format("==★★★Reids异常，操作失败："), e);
			return null;
		}
		
	}

	public boolean isExpires() {
		
		try {
			String accesstime = RedisUtil.get(CacheConsts.WX + "accesstime");
			logger.info("从redis中获取生成微信accessToken的时间!!!!" + accesstime);
			if(StringUtil.isNotEmpty(accesstime)){
				long accesstimeLong = Long.parseLong(accesstime);
				long secends = System.currentTimeMillis() - accesstimeLong;
				logger.info("生成微信accessToken的时间与实时时间差!!!!" + accesstime);
				return secends > 1700 * 1000 ? true : false;
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(String.format("==★★★Reids异常，操作失败："), e);
		    return false;
		}
		
	}

	/**
	 * 获取JSSDKTicket
	 * @return
	 */
	@Override
	public String getJSSDKTicket() {

		AccessTokenPojo accessToken = this.getAccessToken();
		String token = accessToken.getAccessToken();

		String jsapi_ticket = RedisUtil.get(CacheConsts.WX + "jsapi_ticket");
		logger.info("从redis中获取微信jsapi_ticket!!!!" + jsapi_ticket);
		if (StringUtil.isEmpty(jsapi_ticket)) {
			logger.info("重新调用微信获取jsapi_ticket!!!!");
			logger.info("获取公众号JDK---JSSDKTicket：初始化参数" + accessToken);
			String jssdkTicket = WeixinUtil.getJSSDKTicket(token);
			logger.info("获取公众号JDK---JSSDKTicket：初始化参数" + jssdkTicket);
			RedisUtil.set(CacheConsts.WX + "jsapi_ticket", 7100, jssdkTicket);
			return jssdkTicket;
		} else {
			return jsapi_ticket;
		}

	}

	/**
	 * 临时二维码
	 * @param expireSeconds 有效期
	 * @param scene_id  参数
	 * @return
	 */
	@Override
	public String getTicketQrCode(String expireSeconds, Integer scene_id) {

		String token = this.getAccessToken().getAccessToken();
		String valueOf = String.valueOf(scene_id);
		logger.info("创建临时二维码 参数：：expireSeconds" + expireSeconds + "scene_id :"+ scene_id);
		TicketPojo ticketPojo = WeixinUtil.createTemporaryQrCode(token,expireSeconds,valueOf);
		logger.info("创建临时带参二维码成功,ticket=" + ticketPojo.getTicket());
		System.out.println(ticketPojo.getUrl());
		String qrCode = WeixinUtil.chageQrCode(ticketPojo.getTicket());
		logger.info("获取参数二维码路劲,qrCode=" + qrCode);
		System.out.println(qrCode);
		return qrCode;
	}
	
	/**
	 * 临时二维码URL
	 * @param expireSeconds 有效期
	 * @param scene_id  参数
	 * @return
	 */
	@Override
	public String getTicketUrl(String expireSeconds, Integer scene_id) {
		String token = this.getAccessToken().getAccessToken();
		String valueOf = String.valueOf(scene_id);
		logger.info("创建临时二维码 参数：：expireSeconds" + expireSeconds + "scene_id :"+ scene_id);
		TicketPojo ticketPojo = WeixinUtil.createTemporaryQrCode(token,expireSeconds,valueOf);
		logger.info("创建临时带参二维码成功,ticketUrl=" + ticketPojo.getUrl());
		return ticketPojo.getUrl();
	}

	/**
	 * 创建永久二维码
	 * @param scene_id  参数
	 * @return
	 */
	@Override
	public String getTicketsQrCode(String sceneStr) {
		// AccessTokenPojo accessToken = TokenThread.accessToken;
		String token = this.getAccessToken().getAccessToken();
		// uuid = UUID.randomUUID();
		// String string = uuid.toString();
		// String sceneStr = string;
		TicketPojo ticketPojo = WeixinUtil.createPermanentORCode(token,sceneStr);
		String qrCode = WeixinUtil.chageQrCode(ticketPojo.getTicket());

		return qrCode;
	}

	/**
	 * 创建个性化菜单（收银员）
	 * @return
	 */
	public boolean createMenu() {

		// String
		// token="w3MZUu3F9aj0BCI9iSwFNcZNzV6B3fAyC7gtBbryrnra5s9kijen8Ke35tKj2HK8LadGBR42IQZWbJlomNoS5yOFrPYtfSyqYpWu1nGTL5TxBj3Ntw-CyJfgguJ_4vRTWPFiADAJCS";
		String token = this.getAccessToken().getAccessToken();

		boolean flag = WeixinUtil.createIndividuationMenu(getMenu(),token);
		if (flag) {
			logger.info("创建个性化菜单成功      menu" + getMenu().toString());
		} else {
			logger.info("创建个性化菜单失败     《《《《《《《《《《《《《");
		}
		return flag;

	}

	/**
	 *  创建个性化菜单（收款人）
	 * @return
	 */
	public boolean createMenu1() {

		// String
		// token="w3MZUu3F9aj0BCI9iSwFNcZNzV6B3fAyC7gtBbryrnra5s9kijen8Ke35tKj2HK8LadGBR42IQZWbJlomNoS5yOFrPYtfSyqYpWu1nGTL5TxBj3Ntw-CyJfgguJ_4vRTWPFiADAJCS";
		String token = this.getAccessToken().getAccessToken();
		boolean flag = WeixinUtil.createIndividuationMenu(getMenu1(),token);
		if (flag) {
			logger.info("创建个性化菜单成功      menu" + getMenu1().toString());
		} else {
			logger.info("创建个性化菜单失败");
		}

		return flag;
	}
	
	/**
	 *  创建个性化菜单（收款人个体户）
	 * @return
	 */
	public boolean createMenu3() {

		// String
		// token="w3MZUu3F9aj0BCI9iSwFNcZNzV6B3fAyC7gtBbryrnra5s9kijen8Ke35tKj2HK8LadGBR42IQZWbJlomNoS5yOFrPYtfSyqYpWu1nGTL5TxBj3Ntw-CyJfgguJ_4vRTWPFiADAJCS";
		String token = this.getAccessToken().getAccessToken();
		boolean flag = WeixinUtil.createIndividuationMenu(getMenu3(),token);
		if (flag) {
			logger.info("创建个性化菜单成功收款人个体户 menu" + getMenu3().toString());
		} else {
			logger.info("创建个性化菜单失败收款人个体户");
		}

		return flag;
	}

	/**
	 * 删除个性化菜单
	 * @return
	 */
	public boolean deleteMenu() {

		// String
		// token="w3MZUu3F9aj0BCI9iSwFNcZNzV6B3fAyC7gtBbryrnra5s9kijen8Ke35tKj2HK8LadGBR42IQZWbJlomNoS5yOFrPYtfSyqYpWu1nGTL5TxBj3Ntw-CyJfgguJ_4vRTWPFiADAJCS";
		String token = this.getAccessToken().getAccessToken();
		boolean flag = WeixinUtil.deleteMenu(token);

		return flag;
	}
	
	/**
	 *  创建自定义菜单
	 * @return
	 */
	public boolean createMenu2() {
		String token = this.getAccessToken().getAccessToken();
		// String
		// token="w3MZUu3F9aj0BCI9iSwFNcZNzV6B3fAyC7gtBbryrnra5s9kijen8Ke35tKj2HK8LadGBR42IQZWbJlomNoS5yOFrPYtfSyqYpWu1nGTL5TxBj3Ntw-CyJfgguJ_4vRTWPFiADAJCS";
		logger.info("创建自定义菜单 menu");
		boolean flag = WeixinUtil.createMenu(getMenu2(),token);
		if (flag) {
			logger.info("创建自定义菜单成功      menu" + getMenu2().toString());
		} else {
			logger.info("创建自定义菜单失败 ");
		}

		return flag;
	}

	/**
	 * 组装个性化菜单数据1（收银员）
	 * 
	 * @return
	 */
	private static CustomMenu getMenu() {
		/*
		 * ViewButton btn11 = new ViewButton(); btn11.setName("专属活动");
		 * btn11.setType("view");
		 * btn11.setUrl("http://"+APPCONFIG.DOMAIN+"/"+APPCONFIG
		 * .WEBURL+"/h5/oAuchController/exclusiveActivity");
		 */

		/*
		 * ViewButton btn12 = new ViewButton(); btn12.setName("在线服务");
		 * btn12.setType("view");
		 * btn12.setUrl("http://"+APPCONFIG.DOMAIN+"/"+APPCONFIG
		 * .WEBURL+"/h5/oAuchController/onlineServe");
		 */

		ViewButton btn13 = new ViewButton();
		btn13.setName("常见问题");
		btn13.setType("view");
		// btn13.setUrl("http://"+APPCONFIG.DOMAIN+"/"+APPCONFIG.WEBURL+"/h5/oAuchController/commonProblem");
		btn13.setUrl("http://m.soopay.net/c/static/yudcuwnl");

		ViewButton btn21 = new ViewButton();
		btn21.setName("成为代理");
		btn21.setType("view");
		btn21.setUrl(URL1 + "/h5/oAuchController/becomeAgency");

		ViewButton btn22 = new ViewButton();
		btn22.setName("账单明细");
		btn22.setType("view");
		btn22.setUrl(URL+"%2fh5%2foAuchController%2fbillingDetail&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect");

		ViewButton btn23 = new ViewButton();
		btn23.setName("收款");
		btn23.setType("view");
		btn23.setUrl(URL+"%2fh5%2foAuchController%2fgathering&response_type=code&scope=snsapi_base&state=2&connect_redirect=1#wechat_redirect");

		ComplexButton mainBtn1 = new ComplexButton();
		mainBtn1.setName("服务中心");
		mainBtn1.setSub_button(new ViewButton[] { btn13, btn21 });

		/*
		 * ComplexButton mainBtn2 = new ComplexButton();
		 * mainBtn2.setName("服务中心"); mainBtn2.setSub_button(new ViewButton[] {
		 * btn21, btn23, btn23 });
		 */

		/**
		 * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br>
		 * 
		 * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br>
		 * 比如，第三个一级菜单项不是“更多体验”，而直接是“幽默笑话”，那么menu应该这样定义：<br>
		 * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 });
		 */
		/*
		 * Menu menu = new Menu(); menu.setButton(new Button[] { mainBtn1,
		 * mainBtn2 }); menu.setButton(new ViewButton[] { btn13 });
		 */
		CustomMenu menu = new CustomMenu();
		menu.setButton(new Button[] { btn22, btn23, mainBtn1 });
		menu.setMatchrule(new CustomMatchrule(100));
		return menu;
	}

	/**
	 * 组装个性化菜单数据2（收款人）
	 * 
	 * @return
	 */
	private static CustomMenu getMenu1() {

		/*
		 * ViewButton btn11 = new ViewButton(); btn11.setName("专属活动");
		 * btn11.setType("view");
		 * btn11.setUrl("http://"+APPCONFIG.DOMAIN+"/"+APPCONFIG
		 * .WEBURL+"/h5/oAuchController/exclusiveActivity");
		 */

		ViewButton btn13 = new ViewButton();
		btn13.setName("常见问题");
		btn13.setType("view");
		// btn13.setUrl("http://"+APPCONFIG.DOMAIN+"/"+APPCONFIG.WEBURL+"/h5/oAuchController/commonProblem");
		btn13.setUrl("http://m.soopay.net/c/static/yudcuwnl");

		ViewButton btn3 = new ViewButton();
		btn3.setName("收入结算");
		btn3.setType("view");
		btn3.setUrl(URL+"%2fh5%2foAuchController%2fsettlementQueryCard&response_type=code&scope=snsapi_base&state=3&connect_redirect=1#wechat_redirect");

		ViewButton btn12 = new ViewButton();
		btn12.setName("收银员管理");
		btn12.setType("view");
		btn12.setUrl(URL+"%2fh5%2foAuchController%2fcashierManage&response_type=code&scope=snsapi_base&state=4&connect_redirect=1#wechat_redirect");

		ViewButton btn21 = new ViewButton();
		btn21.setName("店铺信息");
		btn21.setType("view");
		btn21.setUrl(URL+"%2fh5%2foAuchController%2fstoreManage&response_type=code&scope=snsapi_base&state=5&connect_redirect=1#wechat_redirect");

		ViewButton btn22 = new ViewButton();
		btn22.setName("账单明细");
		btn22.setType("view");
		btn22.setUrl(URL+"%2fh5%2foAuchController%2fbillingDetail&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect");

		ViewButton btn23 = new ViewButton();
		btn23.setName("收款");
		btn23.setType("view");
		btn23.setUrl(URL+"%2fh5%2foAuchController%2fgathering&response_type=code&scope=snsapi_base&state=2&connect_redirect=1#wechat_redirect");

		ComplexButton mainBtn1 = new ComplexButton();
		mainBtn1.setName("服务中心");
		mainBtn1.setSub_button(new ViewButton[] { btn23, btn12, btn21, btn13 });

		/*
		 * ComplexButton mainBtn2 = new ComplexButton();
		 * mainBtn2.setName("服务中心"); mainBtn2.setSub_button(new ViewButton[] {
		 * btn21, btn23, btn23 });
		 */

		/**
		 * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br>
		 * 
		 * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br>
		 * 比如，第三个一级菜单项不是“更多体验”，而直接是“幽默笑话”，那么menu应该这样定义：<br>
		 * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 });
		 */
		/*
		 * Menu menu = new Menu(); menu.setButton(new Button[] { mainBtn1,
		 * mainBtn2 }); menu.setButton(new ViewButton[] { btn13 });
		 */
		CustomMenu menu = new CustomMenu();
		menu.setButton(new Button[] { btn22, btn3, mainBtn1 });
		menu.setMatchrule(new CustomMatchrule(101));
		return menu;
	}

	/**
	 * 组装个性化菜单数据3（收款人个体户）
	 * 
	 * @return
	 */
	private static CustomMenu getMenu3() {

		/*
		 * ViewButton btn11 = new ViewButton(); btn11.setName("专属活动");
		 * btn11.setType("view");
		 * btn11.setUrl("http://"+APPCONFIG.DOMAIN+"/"+APPCONFIG
		 * .WEBURL+"/h5/oAuchController/exclusiveActivity");
		 */

		ViewButton btn13 = new ViewButton();
		btn13.setName("常见问题");
		btn13.setType("view");
		// btn13.setUrl("http://"+APPCONFIG.DOMAIN+"/"+APPCONFIG.WEBURL+"/h5/oAuchController/commonProblem");
		btn13.setUrl("http://m.soopay.net/c/static/yudcuwnl");

		ViewButton btn2 = new ViewButton();
		btn2.setName("聚财通");
		btn2.setType("view");
		btn2.setUrl(URL+"%2fh5%2foAuchController%2faccumulateThrough&response_type=code&scope=snsapi_base&state=6&connect_redirect=1#wechat_redirect");
		
		ViewButton btn3 = new ViewButton();
		btn3.setName("收入结算");
		btn3.setType("view");
		btn3.setUrl(URL+"%2fh5%2foAuchController%2fsettlementQueryCard&response_type=code&scope=snsapi_base&state=3&connect_redirect=1#wechat_redirect");

		ViewButton btn12 = new ViewButton();
		btn12.setName("收银员管理");
		btn12.setType("view");
		btn12.setUrl(URL+"%2fh5%2foAuchController%2fcashierManage&response_type=code&scope=snsapi_base&state=4&connect_redirect=1#wechat_redirect");

		ViewButton btn21 = new ViewButton();
		btn21.setName("店铺信息");
		btn21.setType("view");
		btn21.setUrl(URL+"%2fh5%2foAuchController%2fstoreManage&response_type=code&scope=snsapi_base&state=5&connect_redirect=1#wechat_redirect");

		ViewButton btn22 = new ViewButton();
		btn22.setName("账单明细");
		btn22.setType("view");
		btn22.setUrl(URL+"%2fh5%2foAuchController%2fbillingDetail&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect");

		ViewButton btn23 = new ViewButton();
		btn23.setName("收款");
		btn23.setType("view");
		btn23.setUrl(URL+"%2fh5%2foAuchController%2fgathering&response_type=code&scope=snsapi_base&state=2&connect_redirect=1#wechat_redirect");

		ComplexButton mainBtn1 = new ComplexButton();
		mainBtn1.setName("服务中心");
		mainBtn1.setSub_button(new ViewButton[] { btn23, btn12, btn21, btn13 });
		
		ComplexButton mainBtn2 = new ComplexButton();
		mainBtn2.setName("收入结算");
		mainBtn2.setSub_button(new ViewButton[] { btn2, btn3 });

		/*
		 * ComplexButton mainBtn2 = new ComplexButton();
		 * mainBtn2.setName("服务中心"); mainBtn2.setSub_button(new ViewButton[] {
		 * btn21, btn23, btn23 });
		 */

		/**
		 * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br>
		 * 
		 * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br>
		 * 比如，第三个一级菜单项不是“更多体验”，而直接是“幽默笑话”，那么menu应该这样定义：<br>
		 * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 });
		 */
		/*
		 * Menu menu = new Menu(); menu.setButton(new Button[] { mainBtn1,
		 * mainBtn2 }); menu.setButton(new ViewButton[] { btn13 });
		 */
		CustomMenu menu = new CustomMenu();
		menu.setButton(new Button[] { btn22, mainBtn2, mainBtn1 });
		menu.setMatchrule(new CustomMatchrule(103));
		return menu;
	}

	
	/**
	 * 组装自定义菜单数据（默认角色）
	 * 
	 * @return
	 */
	private static Menu getMenu2() {
		/*
		 * ViewButton btn11 = new ViewButton(); btn11.setName("专属活动");
		 * btn11.setType("view");
		 * btn11.setUrl("http://"+APPCONFIG.DOMAIN+"/"+APPCONFIG
		 * .WEBURL+"/h5/oAuchController/exclusiveActivity");
		 */

		/*
		 * ViewButton btn12 = new ViewButton(); btn12.setName("在线服务");
		 * btn12.setType("view");
		 * btn12.setUrl("http://"+APPCONFIG.DOMAIN+"/"+APPCONFIG
		 * .WEBURL+"/h5/oAuchController/onlineServe");
		 */

		ViewButton btn13 = new ViewButton();
		btn13.setName("常见问题");
		btn13.setType("view");
		// btn13.setUrl("http://"+APPCONFIG.DOMAIN+"/"+APPCONFIG.WEBURL+"/h5/oAuchController/commonProblem");
		btn13.setUrl("http://m.soopay.net/c/static/yudcuwnl");

		ViewButton btn21 = new ViewButton();
		btn21.setName("成为代理");
		btn21.setType("view");
		btn21.setUrl(URL1+"/h5/oAuchController/becomeAgency");

		ViewButton btn22 = new ViewButton();
		btn22.setName("产品介绍");
		btn22.setType("view");
		btn22.setUrl(URL1+"/h5/oAuchController/productIntroduce");

		ViewButton btn23 = new ViewButton();
		btn23.setName("商户开通");
		btn23.setType("view");
		btn23.setUrl(URL1+"/h5/oAuchController/becomePartnery");
		
		ViewButton btn24 = new ViewButton();
		btn24.setName("商户登录");
		btn24.setType("view");
		btn24.setUrl(URL+"%2fh5%2foAuchController%2fregister&response_type=code&scope=snsapi_base&state=7&connect_redirect=1#wechat_redirect");

		ComplexButton mainBtn1 = new ComplexButton();
		mainBtn1.setName("快速入门");
		mainBtn1.setSub_button(new ViewButton[] { btn13, btn21 });

		ComplexButton mainBtn2 = new ComplexButton();
		mainBtn2.setName("开通/登录");
		mainBtn2.setSub_button(new ViewButton[] { btn24, btn23 });
		
		/**
		 * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br>
		 * 
		 * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br>
		 * 比如，第三个一级菜单项不是“更多体验”，而直接是“幽默笑话”，那么menu应该这样定义：<br>
		 * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 });
		 */
		Menu menu = new Menu();
		menu.setButton(new Button[] { btn22, mainBtn2, mainBtn1 });
		System.out.println(menu);
		return menu;
	}

	/**
	 * 查询菜单
	 * @return
	 */
	@Override
	public String demandMenu() {

		String token = this.getAccessToken().getAccessToken();
		// 调用接口创建菜单
		String menu = WeixinUtil.demandMenu(token);
		System.out.println(menu);
		logger.info("微信公众号|获取自定义菜单=======" + menu + "========");
		return menu;
	}

	/**
	 * 移动用户分组
	 * @return
	 */
	@Override
	public boolean addUserGroup(String openid, int to_groupid) {

		String token = this.getAccessToken().getAccessToken();
		// String token = TokenThread.accessToken.getAccessToken();
		logger.info("微信公众号|移动用户分组接口开始=========token======" + token+ "to_groupid=====" + to_groupid + "openid====" + openid);
		boolean flag = weChatGroupUtil.addUserGroup(token,openid,to_groupid);
		logger.info("微信公众号|移动用户分组接口结束=====flag====是否成功========" + flag);
		return flag;
	}

	/**
	 * 发送客服消息
	 * @return
	 */
	@Override
	public boolean sendMessage(String openid, String text) {

		// String token = TokenThread.accessToken.getAccessToken();
		String token = this.getAccessToken().getAccessToken();
		logger.info("发送客服消息接口开始======WeixinServiceImpl===sendMessage=======");
		boolean flag = WeixinUtil.sendMessage(token,openid,text);
		logger.info("发送客服消息接口结束======WeixinServiceImpl===sendMessage==="+ flag + "====");
		return flag;
	}

	/**
	 * 添加客服帐号
	 * @return
	 */
	@Override
	public boolean addCustomer(String kf_account,String nickname,String password) {
		String token = this.getAccessToken().getAccessToken();
		logger.info("添加客服帐号接口开始======WeixinServiceImpl===addCustomer======="+ token);
		boolean flag = WeixinUtil.addCustomer(token,kf_account,nickname,password);
		logger.info("添加客服帐号接口结束======WeixinServiceImpl===addCustomer=="+flag+"=====");
		return flag;
	}

	/**
	 * 获得模板ID
	 * 
	 * @return
	 */
	@Override
	public String getIndustry() {

		// String token = TokenThread.accessToken.getAccessToken();
		String token = this.getAccessToken().getAccessToken();
		String industry = WeixinUtil.getIndustry(token);
		logger.info("获得模板ID接口结束======WeixinServiceImpl===getIndustry=="+ industry + "=====");

		return industry;
	}

	/**
	 * 获得模板ID **必传 (APPCONFIG.java中找) touser:接收者openid, **必传
	 * template_id:模板ID,**必传 url:,模板跳转链接,
	 * 
	 * 更换提现卡通知 支付成功通知 银行名称：keyword1 订单编号：keyword1 卡号后四位：keyword2 消费金额：keyword2
	 * 操作时间：keyword3 消费门店：keyword3 付款用户：keyword4 付款方式：keyword5
	 * 
	 * 
	 * 商户审核通知 (失败) 商户审核通知(成功) 申请编号：keyword1 申请编号：keyword1 商户简称：keyword2
	 * 商户简称：keyword2 业务员：keyword3 业务员：keyword3 审核结果：keyword4 审核结果：keyword4
	 * 未通过原因：keyword5
	 * 
	 */
	@Override
	public boolean sendTemplateMessage1(Map<String, String> map) {

		// 模板编号
		String template_id = map.get("template_id"); // 必传
		WxTemplate temp = new WxTemplate();
		if (map.get("url") != null || !"".equals(map.get("url"))) {
			temp.setUrl(map.get("url"));
		}
		temp.setTouser(map.get("openid"));
		temp.setTopcolor("#000000");
		temp.setTemplate_id(map.get("template_id"));

		Map<String, TemplateDate> m = new HashMap<String, TemplateDate>();

		TemplateDate keyword1 = new TemplateDate();
		keyword1.setColor("#000000");
		keyword1.setValue(map.get("keyword1"));
		m.put("keyword1", keyword1);
		TemplateDate keyword2 = new TemplateDate();
		keyword2.setColor("#000000");
		keyword2.setValue(map.get("keyword2"));
		m.put("keyword2", keyword2);
		TemplateDate keyword3 = new TemplateDate();
		keyword3.setColor("#000000");
		keyword3.setValue(map.get("keyword3"));
		m.put("keyword3", keyword3);

		String templateId1 = PropertyUtil.getProperties("umfWeChatPlat_appConf.properties").getProperty("saas.wx.templateId1");
		String templateId2 = PropertyUtil.getProperties("umfWeChatPlat_appConf.properties").getProperty("saas.wx.templateId2");

		if (template_id.equals(templateId2)) { // 支付成功通知

			logger.info("发送模板消息=======支付成功通知===sendTemplateMessage===");
			TemplateDate keyword4 = new TemplateDate();
			keyword4.setColor("#000000");
			keyword4.setValue(map.get("keyword4"));
			m.put("keyword4", keyword4);
			TemplateDate keyword5 = new TemplateDate();
			keyword5.setColor("#000000");
			keyword5.setValue(map.get("keyword5"));
			m.put("keyword5", keyword5);
			TemplateDate keyword6 = new TemplateDate();
			keyword6.setColor("#000000");
			keyword6.setValue("点击查看订单详情");
			m.put("remark", keyword6);
			TemplateDate first = new TemplateDate();
			first.setColor("#000000");
			first.setValue("您的支付已成功,如有疑问请联系客服");
			m.put("first", first);
		} else if (template_id.equals(templateId1)) {// 更换提现卡通知
			logger.info("发送模板消息=======更换提现卡通知===sendTemplateMessage===");
			TemplateDate first = new TemplateDate();
			first.setColor("#000000");
			first.setValue("您的提现卡已修改成功,如有疑问请联系客服");
			m.put("first", first);

		}
		temp.setData(m);
		String jsonString = JSONObject.fromObject(temp).toString();
		// 获取access_token
		String token = this.getAccessToken().getAccessToken();

		logger.info("发送模板消息接口开始=======WeixinServiceImpl===sendTemplateMessage==="+ jsonString + "==token===" + token);
		boolean flag = WeixinUtil.sendTemplateMessage(token,jsonString);
		logger.info("发送模板消息接口结束======WeixinServiceImpl===sendTemplateMessage=="+ flag + "=====");
		return flag;
	}

	/**
	 * 发送模板消息
	 * 
	 * @return
	 */
	@Override
	public boolean sendTemplateMessage(String openid, String template_id,
			String url, String amount, String payTime, String payType) {
		// 获取access_token
		String token = this.getAccessToken().getAccessToken();

		Map<String, Object> params1 = new HashMap<String, Object>();
		params1.put("value", "交易成功");
		params1.put("color", "#173177");

		Map<String, Object> params3 = new HashMap<String, Object>();
		params3.put("value", payType);
		params3.put("color", "#173177");
		Map<String, Object> params4 = new HashMap<String, Object>();
		params4.put("value", amount);
		params4.put("color", "#173177");
		Map<String, Object> params5 = new HashMap<String, Object>();
		params5.put("value", payTime);
		params5.put("color", "#173177");

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("first", params1);
		params.put("payType", params3);
		params.put("amount", params4);
		params.put("payTime", params5);
		Map<String, Object> params2 = new HashMap<String, Object>();
		params2.put("touser", openid);
		params2.put("template_id", template_id);
		params2.put("url", url);
		params2.put("data", params);

		String string = JSONObject.fromObject(params2).toString();
		logger.info("发送模板消息接口开始=======WeixinServiceImpl===sendTemplateMessage==="+ params2);
		boolean flag = WeixinUtil.sendTemplateMessage(token, string);
		logger.info("发送模板消息接口结束======WeixinServiceImpl===sendTemplateMessage=="+flag+"=====");
		return flag;
	}
	/**
	 * 发送待签约模板消息
	 * 
	 * @return
	 */
	@Override
	public boolean sendSignTemplateMessage(String openid, String template_id,
		String storeName, String alipayRate, String wechatRate) {
		// 获取access_token
		String token = this.getAccessToken().getAccessToken();

		Map<String, Object> params1 = new HashMap<String, Object>();
		params1.put("value", storeName);
		params1.put("color", "#173177");
		Map<String, Object> params2 = new HashMap<String, Object>();
		params2.put("value", alipayRate);
		params2.put("color", "#173177");
		Map<String, Object> params3 = new HashMap<String, Object>();
		params3.put("value", wechatRate);
		params3.put("color", "#173177");
		Map<String, Object> params4 = new HashMap<String, Object>();
		params4.put("value", "★请点击详情立即开通收款★狂戳详情↓↓↓");
		params4.put("color", "#173177");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("keyword1", params1);
		params.put("keyword2", params2);
		params.put("keyword3", params3);
		params.put("remark", params4);
		Map<String, Object> paramsBig = new HashMap<String, Object>();
		paramsBig.put("touser", openid);
		paramsBig.put("template_id", template_id);
		paramsBig.put("url", URL_SIGN);
		paramsBig.put("data", params);
		String string = JSONObject.fromObject(paramsBig).toString();
		logger.info("发送模板消息接口开始=======WeixinServiceImpl===sendTemplateMessage==="+ string);
		boolean flag = WeixinUtil.sendTemplateMessage(token, string);
		logger.info("发送模板消息接口结束======WeixinServiceImpl===sendTemplateMessage=="+flag+"=====");
		return flag;
	}

	/**
	 * 创建分组
	 * @return
	 */
	@Override
	public Map<String, Object> createGroup(String groupName) {
		String token = this.getAccessToken().getAccessToken();
		//String token="Plc6cWa69ZxbLHyilo1-_kSBT4W5j09DfFkrugu9GVFChQn0t84frv27VeDYe_9BvCastb6ZU5zgK07jKFrpu15ZWVSwuQ7-zvwGES2ca5RAQQdCRVo-cTiLUXyQu41PYXVjAHAJIG";
		//token="";
		Map<String, Object> map = weChatGroupUtil.createGroup(token,groupName);
		return map;
	}
	/**
	 * 查询分组
	 * @return
	 */
	@Override
	public Map<String, Object> getGroup() {
		String token = this.getAccessToken().getAccessToken();
	   // String token="Plc6cWa69ZxbLHyilo1-_kSBT4W5j09DfFkrugu9GVFChQn0t84frv27VeDYe_9BvCastb6ZU5zgK07jKFrpu15ZWVSwuQ7-zvwGES2ca5RAQQdCRVo-cTiLUXyQu41PYXVjAHAJIG";
	    Map<String, Object> map = weChatGroupUtil.getGroup(token);
	    return map;
	}
	
	/**
	 * 删除分组
	 * @return
	 */
	@Override
	public Map<String, Object> deleteGroup(Integer id) {
		String token = this.getAccessToken().getAccessToken();
	    //String token="Plc6cWa69ZxbLHyilo1-_kSBT4W5j09DfFkrugu9GVFChQn0t84frv27VeDYe_9BvCastb6ZU5zgK07jKFrpu15ZWVSwuQ7-zvwGES2ca5RAQQdCRVo-cTiLUXyQu41PYXVjAHAJIG";
	    Map<String, Object> map = weChatGroupUtil.deleteGroup(token,id);
	    return map;
	}
	
	/**
	 * 查看用户标签
	 * @return
	 */
	@Override
	public Map<String, Object> getUserGroup(String openid) {
		String token = this.getAccessToken().getAccessToken();
	    //String token="Plc6cWa69ZxbLHyilo1-_kSBT4W5j09DfFkrugu9GVFChQn0t84frv27VeDYe_9BvCastb6ZU5zgK07jKFrpu15ZWVSwuQ7-zvwGES2ca5RAQQdCRVo-cTiLUXyQu41PYXVjAHAJIG";
	    Map<String, Object> map = weChatGroupUtil.getUserGroup(token,openid);
	    return map;
	}
	
	/**
	 * 删除个性化菜单
	 * @return
	 */
	@Override
	public boolean delconditionalMenu(String menuid) {
		String token = this.getAccessToken().getAccessToken();
		boolean flag = WeixinUtil.delconditionalMenu(token,menuid);
		return flag;
	}
}